package mx.frantor.catalogos.daoimp;

import static mx.frantor.security.util.EncriptaCadena.*;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import mx.frantor.catalogos.dao.MateriaPrimaPassDAO;
import mx.frantor.catalogos.entitys.CatalogoProductoEntity;
import mx.frantor.catalogos.entitys.ClaveUsrEntity;
import mx.frantor.catalogos.entitys.DetallePedidoEntity;
import mx.frantor.util.HibernateUtil;

public class ClaveUsrDAOImpl implements MateriaPrimaPassDAO{

	// Hash de encriptacion : MD5, SHA1, SHA256, SHA384, SHA512
		private final String hash = SHA256; // <-Constante para definir el nivel de Encriptado
		
	@Override
	public String agregarPassMP(ClaveUsrEntity nuevoClaveUsr) {
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		
		session.beginTransaction();
		try {
		session.save(nuevoClaveUsr);
		session.flush();
		session.getTransaction().commit();
		e = nuevoClaveUsr.getIdClave().toString();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();

			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("agregar usuarios resultado " + e);

		return e;
	}

	@Override
	public String modificarPassMp(ClaveUsrEntity modificarClaveUsr) {
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
	
		session.beginTransaction();
		try {
			ClaveUsrEntity claveUsrTemp = (ClaveUsrEntity) session.get(
					ClaveUsrEntity.class, modificarClaveUsr.getIdClave());
		session.flush();
		session.evict(claveUsrTemp);
		session.update(modificarClaveUsr);
		session.getTransaction().commit();

		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();

			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("modificar usuarios resultado " + e);

		return e;
	}

	@Override
	public String eliminarPassMp(long idClaveUsr) {
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		

			// TODO Auto-generated method stub			
			session.beginTransaction();
			try {
				ClaveUsrEntity claveUsrElimina = (ClaveUsrEntity) session.get(
						ClaveUsrEntity.class, idClaveUsr);
			session.delete(claveUsrElimina);
			session.getTransaction().commit();

		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();

			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("eliminar usuarios resultado " + e);

		return e;
	}


	@SuppressWarnings("unchecked")
	@Override
	public ClaveUsrEntity getUsuarioId(long idClaveUsr) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ClaveUsrEntity> usuario = (List<ClaveUsrEntity>) session.createCriteria(ClaveUsrEntity.class)
				.add(Restrictions.eq("IdUsuario", idClaveUsr))
				.setFetchSize(1).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		if(usuario==null){
			return null;
		}else{
			return usuario.get(0);
		}
	}

	@Override
	public ClaveUsrEntity validaUsuario(long IdUsuario, String inPwd){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		@SuppressWarnings("unchecked")
		List<ClaveUsrEntity> usrValido = session.createCriteria(ClaveUsrEntity.class)
					.add(Restrictions.eq("Idusuario", IdUsuario))
					.add(Restrictions.eq("Password",getDigest(inPwd, hash)))
					.setFetchSize(1).list();
		session.getSessionFactory().getCurrentSession().close();
		try{
			return usrValido.get(0);
		}catch(Exception err){
			System.out.println("El usuario "+ IdUsuario +" no existe o es invalido: " + err.getMessage());
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public ClaveUsrEntity getUsuariopassPorIdusuario(long idUsr) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ClaveUsrEntity> catalogop = (List<ClaveUsrEntity>) session.createCriteria(ClaveUsrEntity.class)
				.add(Restrictions.eq("Idusuario", idUsr))
				.setFetchSize((int) 1).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		if(catalogop.isEmpty()){
			System.out.println("No encontro");
			return null;
		}else{
			return catalogop.get(0);
		}	
	}
}
